package com.mes.inventory.domain;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.OrderBy;
import org.springframework.context.annotation.Lazy;

import com.mes.basicdata.domain.Dept;
import com.mes.basicdata.domain.Employee;
import com.mes.common.domain.BasicModel;
import com.mes.task.domain.Task;

/*import com.mes.basicdata.persistence.domain.Dept;
import com.mes.basicdata.persistence.domain.Employee;
import com.mes.basicdata.persistence.domain.Part;
import com.mes.basicdata.persistence.domain.Routing;
import com.mes.basicdata.persistence.domain.TypesData;
import com.mes.task.persistence.domain.Task;*/

/**
 * <p>
 * InvIoHeader实体，代表库存出入库表头。
 * </p>
 * 
 */
@Entity()
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "I_IOHEADER")
@BatchSize(size = 20)
@Lazy(value = true)
public class InvIO extends BasicModel {
	public final static int IOTYPE_IN_NORMAL = 19; // 一般入库
	public final static int IOTYPE_OUT_NORMAL = 29; // 一般出库
	
	public final static int IOTYPE_IN_BAOFEI = 18; // 报废入库
	public final static int IOTYPE_OUT_BAOFEI = 28; // 报废出库
	
	public final static int IOTYPE_IN_PANDIAN = 17; // 盘盈入库
	public final static int IOTYPE_OUT_PANDIAN = 27; // 盘亏出库
	
	public final static int IOTYPE_IN_CHECK = 16; // 检验入库
	public final static int IOTYPE_OUT_CHECK = 26; // 检验出库
	
	public final static int IOTYPE_OUT_USE = 24; // 领用出库
	
	public final static int IOTYPE_IN_DIAOBO = 13; // 调拨入库
	public final static int IOTYPE_OUT_DIAOBO = 23; // 调拨出库
	
	public final static int IOTYPE_IN_RETURN = 12; // 归还入库
	public final static int IOTYPE_OUT_BORROW = 22; // 借用出库
	
	public final static int IOTYPE_IN_ZANCUN = 101; // 暂存入库
	public final static int IOTYPE_OUT_ZANCUN = 201; // 暂存出库
	
	public final static int IOTYPE_IN_WANGONG = 102; // 完工入库
	public final static int IOTYPE_IN_CAIGOU = 103; // 采购入库
	
	public final static int IOTYPE_IN_WEIWAI = 105; // 委外、外协入库
	public final static int IOTYPE_OUT_WEIWAI = 205; // 委外、外协出库
	
	public final static int IOTYPE_IN_XIEZUO = 106; // 协作入库
	public final static int IOTYPE_OUT_XIEZUO = 206; // 协作出库
	
	public final static int IOTYPE_IN_XIEZUOTUIHUI = 107; // 协作退回入库
	public final static int IOTYPE_OUT_XIEZUOTUIHUI = 207; // 协作退回出库
	
	public final static int IOTYPE_IN_TUIKU = 109; // 退库
	
	
	public final static int IOTYPE_IN_ZHOUZHUAN = 111; // 周转收料
	public final static int IOTYPE_OUT_ZHOUZHUAN = 211; // 周转发料
	
	public final static int IOTYPE_IN_JIANYAN = 112; // 检验收料
	public final static int IOTYPE_OUT_JIANYAN = 212; // 检验发料
	
	
	public final static int IOSTATE_CREATED = 1; // 已创建、草稿
	public final static int IOSTATE_AUDITED = 2; // 审核通过
	public final static int IOSTATE_NOAUDITED = 3; // 审核未通过 
	public final static int IOSTATE_APPLIED = 4; // 申请出入库
	public final static int IOSTATE_ACCOUNTED = 5; // 已登帐
	public final static int IOSTATE_CLOSED = 10; // 已结案
	
	@Id
	@GenericGenerator(name = "uniqueId", strategy = "assigned")
	@Column(name = "UNIQUEID")
	protected String uniqueId;

	/**
	 * 出入库单号
	 */
	@Column(name = "INVIOID")
	protected String invIOId;
	
	// 出入库单据明细
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "invIO")
	@OrderBy(clause = "SEQ asc")
	private Set<InvIODetail> details;

	/**
	 * 出入库类型
	 */
	/**
	 * 获取出入库类型描述 205：委外出库 105：协作入库 152：拆分 103：采购入库 12：归还入库 212：检验发料 112：检验收料
	 * 211：周转发料 111：周转收料 109：退库 107：协作退回入库 207：协作退回出库 101：暂存入库 23：调拨出库 22：借用出库
	 * 201：暂存出库 19：一般入库 18：报废入库 17：盘盈入库 16：检验入库 15：工具入库 14：交付入库 29：一般出库 28：报废出库
	 * 27：盘亏出库 26：检验出库 25：工具出库 24：生产领料 13：调拨入库 11：入库 102：完工入库 151：装配 106：委外入库
	 * 206：协作出库
	 */
	@Column(name = "IOTYPE")
	protected Integer ioType;
	
//	@ForeignKey(name = "null")
//	@ManyToOne(fetch = FetchType.LAZY)
//	@JoinColumnsOrFormulas(value = {
//			@JoinColumnOrFormula(column = @JoinColumn(name = "TASKTYPE", referencedColumnName = "ID", nullable = true, insertable = false, updatable = false)),
//			@JoinColumnOrFormula(formula = @JoinFormula(value = "'invtype'", referencedColumnName = "TYPESNAME")) })
//	protected TypesData ioTypeDesc;

	/**
	 * 出入库状态
	 */
	// 获取出入库状态描述 1：草稿 2：审核通过 4：已预出入库 6：已结案 9：已结转 5：已记帐 3：审核未通过
	@Column(name = "IOSTATE")
	protected Integer ioState;
	
//	@ForeignKey(name = "null")
//	@ManyToOne(fetch = FetchType.LAZY)
//	@JoinColumnsOrFormulas(value = {
//			@JoinColumnOrFormula(column = @JoinColumn(name = "TASKTYPE", referencedColumnName = "ID", nullable = true, insertable = false, updatable = false)),
//			@JoinColumnOrFormula(formula = @JoinFormula(value = "'iostate'", referencedColumnName = "TYPESNAME")) })
//	protected TypesData ioStateDesc;

	/**
	 * 出入库原因
	 */
	@Column(name = "REASONCODE")
	protected String reasonCode;

	/**
	 * 库房编号
	 */
	@Column(name = "WAREHOUSEID")
	protected String warehouseUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "WAREHOUSEID", insertable = false, updatable = false)
	protected Warehouse warehouse;

	/**
	 * 库管员编号
	 */
	@Column(name = "WAREHOUSEMANAGER")
	protected String warehouseManagerUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "WAREHOUSEMANAGER", insertable = false, updatable = false)
	protected Employee warehouseManager;

	/**
	 * 出入库日期
	 */
	@Column(name = "IODATE")
	protected Date ioDate;

	/**
	 * 部门编号
	 */
	@Column(name = "DEPTID")
	protected String deptUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "DEPTID", insertable = false, updatable = false)
	protected Dept dept;
	
	/**
	 * 生成厂家编号
	 */
	@Column(name = "SUPPLIER")
	protected String supplierUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "SUPPLIER", insertable = false, updatable = false)
	protected Dept supplier;

	

	/**
	 * 出入库员工编号
	 */
	@Column(name = "DEPTEMP")
	protected String ioEmployeeUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "DEPTEMP", insertable = false, updatable = false)
	protected Employee ioEmployee;

	/**
	 * 总金额
	 */
	@Column(name = "TOTALAMOUNT")
	protected Float totalAmount;
	/**
	 * 发票号
	 */
	@Column(name = "INVOICEID")
	protected String invoiceId;

	/**
	 * 客户编号
	 */
	@Column(name = "PARTNERID")
	protected String partnerId;

	/**
	 * 该出入库单关联的任务号
	 */
	@Column(name = "TASKUID")
	protected String taskUid;
	// many-to-one
	@ForeignKey(name = "null")
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "TASKUID", insertable = false, updatable = false)
	protected Task task;
	
	/**
	 * 合同编号
	 */
	@Column(name = "CONTRACTUID")
	protected String contractUid;
	
	/**
	 * 合同序列号
	 */
	@Column(name = "CONTRACTSEQ")
	protected String contractSeq;

	

	/** default constructor */
	public InvIO() {
	}

	@Override
	public String getUuid() {
		// TODO Auto-generated method stub
		return this.getUniqueId();
	}

	@Override
	public void setUuid(String uuid) {
		// TODO Auto-generated method stub
		this.setUniqueId(uuid);
	}

	public String getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId(String uniqueId) {
		this.uniqueId = uniqueId;
	}

	public String getInvIOId() {
		return invIOId;
	}

	public void setInvIOId(String invIOId) {
		this.invIOId = invIOId;
	}

	public Set<InvIODetail> getDetails() {
		return details;
	}

	public void setDetails(Set<InvIODetail> details) {
		this.details = details;
	}

	public Integer getIoType() {
		return ioType;
	}

	public void setIoType(Integer ioType) {
		this.ioType = ioType;
	}

	public Integer getIoState() {
		return ioState;
	}

	public void setIoState(Integer ioState) {
		this.ioState = ioState;
	}

	public String getReasonCode() {
		return reasonCode;
	}

	public void setReasonCode(String reasonCode) {
		this.reasonCode = reasonCode;
	}

	public String getWarehouseUid() {
		return warehouseUid;
	}

	public void setWarehouseUid(String warehouseUid) {
		this.warehouseUid = warehouseUid;
	}

	public Warehouse getWarehouse() {
		return warehouse;
	}

	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	public String getWarehouseManagerUid() {
		return warehouseManagerUid;
	}

	public void setWarehouseManagerUid(String warehouseManagerUid) {
		this.warehouseManagerUid = warehouseManagerUid;
	}

	public Employee getWarehouseManager() {
		return warehouseManager;
	}

	public void setWarehouseManager(Employee warehouseManager) {
		this.warehouseManager = warehouseManager;
	}

	public Date getIoDate() {
		return ioDate;
	}

	public void setIoDate(Date ioDate) {
		this.ioDate = ioDate;
	}

	public String getDeptUid() {
		return deptUid;
	}

	public void setDeptUid(String deptUid) {
		this.deptUid = deptUid;
	}

	public Dept getDept() {
		return dept;
	}

	public void setDept(Dept dept) {
		this.dept = dept;
	}

	public String getIoEmployeeUid() {
		return ioEmployeeUid;
	}

	public void setIoEmployeeUid(String ioEmployeeUid) {
		this.ioEmployeeUid = ioEmployeeUid;
	}

	public Employee getIoEmployee() {
		return ioEmployee;
	}

	public void setIoEmployee(Employee ioEmployee) {
		this.ioEmployee = ioEmployee;
	}

	public Float getTotalAmount() {
		return totalAmount;
	}

	public void setTotalAmount(Float totalAmount) {
		this.totalAmount = totalAmount;
	}

	public String getInvoiceId() {
		return invoiceId;
	}

	public void setInvoiceId(String invoiceId) {
		this.invoiceId = invoiceId;
	}

	public String getPartnerId() {
		return partnerId;
	}

	public void setPartnerId(String partnerId) {
		this.partnerId = partnerId;
	}

	public String getContractUid() {
		return contractUid;
	}

	public void setContractUid(String contractUid) {
		this.contractUid = contractUid;
	}

	public String getContractSeq() {
		return contractSeq;
	}

	public void setContractSeq(String contractSeq) {
		this.contractSeq = contractSeq;
	}

	

	public String getSupplierUid() {
		return supplierUid;
	}

	public void setSupplierUid(String supplierUid) {
		this.supplierUid = supplierUid;
	}

	public Dept getSupplier() {
		return supplier;
	}

	public void setSupplier(Dept supplier) {
		this.supplier = supplier;
	}

	public String getTaskUid() {
		return taskUid;
	}

	public void setTaskUid(String taskUid) {
		this.taskUid = taskUid;
	}

	public Task getTask() {
		return task;
	}

	public void setTask(Task task) {
		this.task = task;
	}
	

}